package Data;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class GoodPresentOperator {


	private ArrayList<GoodPresentData> goodPresentList = new ArrayList<GoodPresentData>();
	String filename = "D:\\GoodPresentRecord.txt";
	
	public GoodPresentOperator(){
		try{
			BufferedReader bf = new BufferedReader(new FileReader(filename));
			GoodPresentData gpd = null;
			String data = "";
			String[] record = null;
			while((data = bf.readLine()) != null){
				if(data.trim().equals("")){
					continue;
				}
				record = splitText(data);
				gpd = new GoodPresentData(Integer.parseInt(record[0]),Integer.parseInt(record[1]),Double.parseDouble(record[2]),record[3],record[4]);
				goodPresentList.add(gpd);
				
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public String[] splitText(String s){
		String[] data = s.split(",");
		return data;
	}
	
	
	public ArrayList<GoodPresentData> getGoodPresentData(){
		return goodPresentList;
	}
	
	public void removeGoodPresent(int goodID,int giftID,double giftNum,String goodPresentStart,String goodPresentEnd){
		String s = null;
		String s1 = "\r\n";
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filename)));
			for(GoodPresentData gpd : goodPresentList){
				if(gpd.getGoodID()==goodID && gpd.getGiftID()==giftID && gpd.getGiftNum()==giftNum
						&& gpd.getGoodPresentStart().equals(goodPresentStart) && gpd.getGoodPresentEnd().equals(goodPresentEnd)){
					continue;
				}
				s = gpd.getGoodID() + "," + gpd.getGiftID() + "," + gpd.getGiftNum() + "," + gpd.getGoodPresentStart() + "," +gpd.getGoodPresentEnd();
				writer.write(s);
				writer.write(s1);
			}
						
			writer.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void savaGoodPresent(int goodID,int giftID,double giftNum,String goodPresentStart,String goodPresentEnd){
		String s = null;
		String s1 = "\r\n";
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filename)));
			goodPresentList.add(new GoodPresentData(goodID, giftID, giftNum, goodPresentStart, goodPresentEnd));
			for(GoodPresentData gpd : goodPresentList){
				s = gpd.getGoodID() + "," + gpd.getGiftID() + "," + gpd.getGiftNum() + "," + gpd.getGoodPresentStart() + "," +gpd.getGoodPresentEnd();
				writer.write(s);
				writer.write(s1);
			}
						
			writer.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}


}
